package com.fangyuzhong.intelliJ.hadoop.fsbrowser.ui;

import com.fangyuzhong.intelliJ.hadoop.core.dispose.DisposableProjectComponent;
import com.fangyuzhong.intelliJ.hadoop.fsconnection.ConnectionHandler;
import com.fangyuzhong.intelliJ.hadoop.fsconnection.ConnectionManager;
import com.fangyuzhong.intelliJ.hadoop.fsbrowser.model.BrowserTreeModel;
import com.fangyuzhong.intelliJ.hadoop.fsbrowser.model.FileSystemBrowserTreeNode;
import com.fangyuzhong.intelliJ.hadoop.fsbrowser.model.SimpleBrowserTreeModel;
import com.fangyuzhong.intelliJ.hadoop.fsbrowser.model.TabbedBrowserTreeModel;
import com.intellij.openapi.util.Disposer;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.util.IncorrectOperationException;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;

/**
 * 定义简单的展示Tree的窗体
 * Created by fangyuzhong on 17-7-16.
 */
public class SimpleBrowserForm
        extends FileSystemBrowserForm
{
    private JPanel mainPanel;
    private JScrollPane browserScrollPane;
    private FileSystemBrowserTree browserTree;

    /**
     * 使用SimpleBrowserTreeModel，初始化
     * @param parentComponent
     */
    public SimpleBrowserForm(DisposableProjectComponent parentComponent)
    {
        this(parentComponent, new SimpleBrowserTreeModel(parentComponent.getProject(), ConnectionManager.getInstance(parentComponent.getProject()).getConnectionBundle()));
    }

    /**
     * TabbedBrowserTreeModel，初始化
     * @param parentComponent
     * @param connectionHandler
     */
    public SimpleBrowserForm(DisposableProjectComponent parentComponent, ConnectionHandler connectionHandler)
    {
        this(parentComponent, new TabbedBrowserTreeModel(connectionHandler));
    }
    /**
     *初始化，使用BrowserTreeModel 创建文件树控件对象FileSystemBrowserTree
     * @param parentComponent
     * @param treeModel
     */
    private SimpleBrowserForm(DisposableProjectComponent parentComponent, BrowserTreeModel treeModel)
    {
        super(parentComponent);
        this.browserTree = new FileSystemBrowserTree(treeModel);
        this.browserScrollPane.setViewportView(this.browserTree);
        this.browserScrollPane.setBorder(new EmptyBorder(1, 0, 0, 0));
        ToolTipManager.sharedInstance().registerComponent(this.browserTree);
        Disposer.register(this, this.browserTree);
    }

    /**
     * 获取文件系统连接处理类
     * @return
     */
    public ConnectionHandler getConnectionHandler()
    {
        if ((this.browserTree.getModel() instanceof TabbedBrowserTreeModel))
        {
            TabbedBrowserTreeModel treeModel = (TabbedBrowserTreeModel) this.browserTree.getModel();
            return treeModel.getConnectionHandler();
        }
        throw new IncorrectOperationException("Multiple fsconnection tabs can not return one fsconnection.");
    }

    public JComponent getComponent()
    {
        return this.mainPanel;
    }

    /**
     * 获取FileSystemBrowserTree对象
     * @return
     */
    public FileSystemBrowserTree getBrowserTree()
    {
        return this.browserTree;
    }

    /**
     * 设置选中树元素
     */
    public void selectElement(FileSystemBrowserTreeNode treeNode, boolean focus, boolean scroll)
    {
        this.browserTree.selectElement(treeNode, focus);
    }

    /**
     * 构建Tree
     */
    public void rebuildTree()
    {
        this.browserTree.getModel().getRoot().rebuildTreeChildren();
    }

    public void dispose()
    {
        super.dispose();
        this.browserTree = null;
    }


    /**
     * @noinspection ALL
     */
    private Font $$$getFont$$$(String fontName, int style, int size, Font currentFont)
    {
        if (currentFont == null) return null;
        String resultName;
        if (fontName == null)
        {
            resultName = currentFont.getName();
        } else
        {
            Font testFont = new Font(fontName, Font.PLAIN, 10);
            if (testFont.canDisplay('a') && testFont.canDisplay('1'))
            {
                resultName = fontName;
            } else
            {
                resultName = currentFont.getName();
            }
        }
        return new Font(resultName, style >= 0 ? style : currentFont.getStyle(), size >= 0 ? size : currentFont.getSize());
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$()
    {
        mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        browserScrollPane = new JBScrollPane();
        mainPanel.add(browserScrollPane, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$()
    {
        return mainPanel;
    }
}
